<template>
  <el-card>
    <div slot="header">
      <div style="display: inline-block; line-height: 45px;">
        <span style="font-size: 18px; font-weight: 600; color: rgb(73, 80, 96);">Contest Announcements</span>
      </div>
      <div style="float: right; line-height: 45px; margin-right: 10px;">
        <div style="display: inline-block;">
          <el-button type="primary" :disabled="buttonDisable" @click="handleRefresh">
            <i :class="buttonStyle"></i>
            <span>Refresh</span>
          </el-button>
        </div>
      </div>
    </div>
    <el-collapse>
      <el-collapse-item v-for="(announcement,index) in model.announcementList" :key="index"
                        :title="announcement.title">
        <my-markdown :content="announcement.content" class="markdown-body"></my-markdown>
      </el-collapse-item>
    </el-collapse>
  </el-card>
</template>

<script>
import {getAnnouncementList} from '@/network/contest';
import MyMarkdown from "@/components/common/markdown/MyMarkdown";

export default {
  components: {MyMarkdown},
  mounted() {
    this.init()
  },
  data() {
    return {
      buttonDisable: false,
      buttonStyle: 'el-icon-refresh',
      model: {
        announcementList: []
      }
    }
  },
  methods: {
    init: function () {
      const contestId = this.$route.params.contestId;
      this.buttonDisable = true;
      this.buttonStyle = 'el-icon-loading';
      getAnnouncementList(contestId).then(res => {
        console.log(res.data)
        this.model.announcementList = res.data;
        this.buttonDisable = false;
        this.buttonStyle = 'el-icon-refresh'
      }).catch(_ => {
        console.log(_);
        this.buttonDisable = false;
        this.buttonStyle = 'el-icon-refresh'
      })
    },
    handleRefresh: function () {
      this.init()
    }
  },
  watch: {
    '$route': function (newVal, oldVal) {
      if (newVal !== oldVal) {
        this.init()
      }
    }
  }
}
</script>

<style>

</style>
